//
//  NSString+YFExtension.m
//  project
//
//  Created by Winny Zhang on 2019/3/23.
//  Copyright © 2019 健健. All rights reserved.
//

#import "NSString+YFExtension.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (YFExtension)

/**
 从字符串格式的日期转换成指定格式的日期字符串

 @param format 指定日期的格式
 @return 日期字符串
 */
- (NSString *)dateStringWithFormat:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:self];
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

/**
 返回二进制 Bytes 流的字符串表示形式

 @param bytes 二进制 Bytes 数组
 @param length 数组长度
 @return 字符串表示形式
 */
- (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length {
    NSMutableString *str = [NSMutableString string];
    for (int i = 0; i < length; i++) {
        [str appendFormat:@"%02x", bytes[i]];
    }
    return str.copy;
}

#pragma mark - 散列函数 加密

/**
 MD5加密

 @return 加密后的字符串
 */
- (NSString *)MD5 {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), buffer);
    return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
}

/**
 SHA1加密

 @return 加密后的字符串
 */
- (NSString *)SHA1 {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(str, (CC_LONG)strlen(str), buffer);
    return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
}

/**
 SHA224加密

 @return 加密后的字符串
 */
- (NSString *)SHA224 {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_SHA224_DIGEST_LENGTH];
    CC_SHA224(str, (CC_LONG)strlen(str), buffer);
    return [self stringFromBytes:buffer length:CC_SHA224_DIGEST_LENGTH];
}

#pragma mark - 散列函数 终止

#pragma mark - 时间戳
/**
 字符串格式的时间转换成时间戳

 @return 时间戳字符串
 */
- (long)timestamp {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:(NSDateFormatterMediumStyle)];
    [formatter setTimeStyle:(NSDateFormatterShortStyle)];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *stringDate = [formatter dateFromString:self];
    long timestamp = (long)[stringDate timeIntervalSince1970];
    
    return timestamp;
}

/**
 根据字体计算 String 的 size
 
 @param font 字体
 @param boundingSize 计算 size 的 标准
 @return size
 */
- (CGSize)sizeWithFont:(UIFont *)font boundingSize:(CGSize)boundingSize {
    return [self boundingRectWithSize:boundingSize
                              options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                           attributes: @{NSFontAttributeName: font}
                              context:nil].size;
}

+ (NSString *)strTimeWithTimestamp:(long long)timestamp {
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    [dateFmt setDateFormat:@"HH:mm"];
    
    return [self formateDate:[dateFmt stringFromDate:msgDate]];
}

+ (NSString *)formateDate:(NSString *)dateString {
    @try {
        // ------实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM/dd HH:mm"];//这里的格式必须和DateString格式一致
        NSDate * nowDate = [NSDate date];
        NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
        if (!needFormatDate) {
            return dateString;
        }
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
        NSString *dateStr = [[NSString alloc] init];
        if (time<=60) {  //1分钟以内的
            dateStr = @"刚刚";
        }else if(time<=60*60){  //一个小时以内的
            int mins = time/60;
            dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
        }else if(time<=60*60*24){  //在两天内的
            [dateFormatter setDateFormat:@"YYYY-MM-dd"];
            NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
            NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
            
            [dateFormatter setDateFormat:@"HH:mm"];
            if ([need_yMd isEqualToString:now_yMd]) {
                //在同一天
                dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }else{
                //昨天
                dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }
        }else if (time>60*60*24 && time<=60*60*24*7) {
            NSDateComponents *componets = [[NSCalendar autoupdatingCurrentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
            NSInteger weekday = [componets weekday];
            if (weekday == 1) {
                dateStr = @"星期日";
            } else if (weekday == 2) {
                dateStr = @"星期一";
            } else if (weekday == 3) {
                dateStr = @"星期二";
            } else if (weekday == 4) {
                dateStr = @"星期三";
            } else if (weekday == 5) {
                dateStr = @"星期四";
            } else if (weekday == 6) {
                dateStr = @"星期五";
            } else if (weekday == 7) {
                dateStr = @"星期六";
            }
        } else {
            [dateFormatter setDateFormat:@"yyyy"];
            NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            if ([yearStr isEqualToString:nowYear]) {
                [dateFormatter setDateFormat:@"MM-dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
                return dateString;
            }else{
                [dateFormatter setDateFormat:@"MM-dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
                return dateString;
            }
        }
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
    
    
}
@end
